package cn.ultrawise.view;

import cn.ultrawise.gameinterface.IView;
import cn.ultrawise.planeshootinggame.PlaneActivity;
import cn.ultrawise.planeshootinggame.R;
import android.R.integer;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.media.MediaPlayer;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;

/**
 * 娓告垙鑿滃崟鐣岄潰
 * 
 * @author Administrator
 * 
 */
public class WelcomeView extends SurfaceView implements IView {
	TutorialThread tutorialThread;
	PlaneActivity planeActivity;
	Paint alphaPaint, paint;
	boolean isSound = true;
	MediaPlayer mediaPlayer;
	int alpha = 55;// 0~255 鏁板�瓒婂皬瓒婇�鏄�
	int style = 0;
	int tableWidth;
	int tableHeigh;
	int xButton; // 鎸夐挳鐨刋鍧愭爣
	int yButtons[] = new int[4];// 鎸夐挳鐨刌杞村潗鏍�
	Bitmap bitmaps[] = new Bitmap[6];// 鍦栫墖闋嗗簭锛氳儗鏅�闁嬪鎸夐垥 骞姪鎸夐垥 鎵撳紑澹伴煶鎸夐挳
										// 鍏抽棴澹伴煶鎸夐挳 缁撴潫鎸夐挳
	int bitmapId[] = new int[] { R.drawable.menu,
			R.drawable.startgame, R.drawable.help, R.drawable.opensound,
			R.drawable.closesound, R.drawable.exit };

	public WelcomeView(Context context, PlaneActivity planeActivity,
			int tableWidth, int tableHeigh) {
		super(context);
		this.tableWidth = tableWidth;
		this.tableHeigh = tableHeigh;
		this.planeActivity = planeActivity;
		initBitMap();
		initXY();
		getHolder().addCallback(this);
		tutorialThread = new TutorialThread(200, this, getHolder());
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub

	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		this.tutorialThread.setFlag(true);
		this.tutorialThread.start();
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		boolean retry = true;
		this.tutorialThread.setFlag(false);
		while (retry) {
			try {
				this.tutorialThread.join();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			retry = false;
		}
	}

	public boolean onTouchEvent(android.view.MotionEvent event) {
		double x = event.getX();
		double y = event.getY();
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			if (x > xButton && x < xButton + bitmaps[1].getWidth()
					&& y > yButtons[0]
					&& y < yButtons[0] + bitmaps[1].getHeight()) {
				System.out.println("start");

			} else if (x > xButton && x < xButton + bitmaps[2].getWidth()
					&& y > yButtons[1]
					&& y < yButtons[1] + bitmaps[2].getHeight()) {
				System.out.println("help");

			} else if (x > xButton && x < xButton + bitmaps[3].getWidth()
					&& y > yButtons[2]
					&& y < yButtons[2] + bitmaps[3].getHeight()) {
				isSound = !isSound;
				System.out.println("sound");

			} else if (x > xButton && x < xButton + bitmaps[4].getWidth()
					&& y > yButtons[3]
					&& y < yButtons[3] + bitmaps[4].getHeight()) {
				System.out.println("exit");

			}

			break;
		}
		return false;
	};

	@Override
	public void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		canvas.drawColor(Color.WHITE);
		if (style == 0) {
			alphaPaint.setAlpha(alpha);
			canvas.drawBitmap(bitmaps[0], 0, 0, alphaPaint);// 缁樺埗鑳屾櫙鍥�
			alpha += 50;
			if (alpha >= 255) {
				style++;
			}
		} else if (style == 1) {
			canvas.drawBitmap(bitmaps[0], 0, 0, paint);// 缁樺埗鑳屾櫙鍥�
			canvas.drawBitmap(bitmaps[1], xButton, yButtons[0], paint);// 缁樺埗寮�娓告垙鎸夐挳
			canvas.drawBitmap(bitmaps[2], xButton, yButtons[1], paint);// 缁樺埗甯姪鎸夐挳
			if (isSound) {
				canvas.drawBitmap(bitmaps[3], xButton, yButtons[2], paint);
			} else {
				canvas.drawBitmap(bitmaps[4], xButton, yButtons[2], paint);
			}
			canvas.drawBitmap(bitmaps[5], xButton, yButtons[3], paint);

		}
		// canvas.drawRect(0, 0, 480, 48, paint);// 缁樺埗涓婁笅鐨勯粦妗�
		// canvas.drawRect(0, 270, 480, 320, paint);
	}

	/*
	 * 瀹炰緥鍖栧潗鏍�
	 */
	public void initXY() {
		xButton = tableWidth / 3 + 15;
		int k = 40; // 鎸夐挳Y杞翠笂鐨勯棿璺�
		for (int i = 0; i < 4; i++) {
			yButtons[i] = tableHeigh / 2 + (k * i);
			this.planeActivity.progressView.progress += 5 ;
		}
	}

	@Override
	public void initBitMap() {
		// TODO Auto-generated method stub
		alphaPaint = new Paint();
		paint = new Paint();
		for (int i = 0; i < bitmaps.length; i++) {
			if (true) {
				bitmaps[i] = BitmapFactory.decodeResource(getResources(),
						bitmapId[i]);
				this.planeActivity.progressView.progress += 5 ;
			}
		}
	}

	/*
	 * 鍒峰抚绾跨▼
	 */
	class TutorialThread extends Thread {
		private int span;// 浼戠湢鏃堕棿
		private WelcomeView welcomeView;
		private boolean flag;
		private SurfaceHolder surfaceHolder;

		public TutorialThread(int span, WelcomeView welcomeView,
				SurfaceHolder surfaceHolder) {
			super();
			this.span = span;
			this.welcomeView = welcomeView;
			this.surfaceHolder = surfaceHolder;
		}

		public boolean isFlag() {
			return flag;
		}

		public void setFlag(boolean flag) {
			this.flag = flag;
		}

		@Override
		public void run() {
			// TODO Auto-generated method stub
			super.run();
			Canvas canvas;
			while (isFlag()) {
				canvas = null;
				try {
					canvas = this.surfaceHolder.lockCanvas(null);
					synchronized (this.surfaceHolder) {
						welcomeView.onDraw(canvas);
					}
				} finally {
					if (canvas != null) {
						surfaceHolder.unlockCanvasAndPost(canvas);
					}
				}
				try {
					Thread.sleep(span);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}
